////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                                            //
//                                                         WNProject                                                          //
//                                                                                                                            //
//            This file is distributed under the BSD 2-Clause open source license. See License.txt for details.               //
//                                                                                                                            //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "WNNetworking/inc/WNBufferResource.h"
#include "WNNetworking/inc/WNNetworkManager.h"

using namespace WNNetworking;
using namespace WNMemory;

WNBufferResource::WNBufferResource(const WNNetworkManager& manager):
    mManager(manager),
    mCurLocation(0) {
    #ifdef _WN_WINDOWS
        mBuffer = static_cast<WN_CHAR*>(WNMalloc(WNContainers::WNDataBuffer::mMaxWNDataBufferWrite));
        mBaseLocation = 0;
        mWinBuf.buf = mBuffer;
        mWinBuf.len = static_cast<ULONG>(mBaseLocation);
    #endif
}

WNBufferResource::~WNBufferResource() {
    WNFree(mBuffer);
}

WN_CHAR* WNBufferResource::GetPointer() {
    return(mBuffer + mCurLocation);
}

WN_VOID WNBufferResource::Clear() {
    mCurLocation = 0;
}

WN_VOID WNBufferResource::AddData(WN_SIZE_T _data) {
    mCurLocation += _data;

    WN_DEBUG_ASSERT(mCurLocation <= WNContainers::WNDataBuffer::mMaxWNDataBufferWrite);
}

WN_CHAR* WNBufferResource::GetBaseLocation() {
    return(mBuffer);
}

WN_SIZE_T WNBufferResource::GetSize() {
    return(WNContainers::WNDataBuffer::mMaxWNDataBufferWrite);
}

WN_SIZE_T WNBufferResource::GetWritten() {
    return(mCurLocation);
}

WN_VOID WNBufferResource::FlushWrite() {
    #ifdef _WN_WINDOWS
        mWinBuf.len = static_cast<ULONG>(mCurLocation);
    #endif
}

WN_VOID WNBufferResource::PrepareRead() {
    #ifdef _WN_WINDOWS
        mWinBuf.len = static_cast<ULONG>(WNContainers::WNDataBuffer::mMaxWNDataBufferWrite - mCurLocation);
        mWinBuf.buf = GetPointer();
    #endif
}